package yt.kpool.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;

import java.math.BigDecimal;
import java.util.List;

import yt.kpool.R;
import yt.kpool.activity.home.HomeSecondAc;
import yt.kpool.bean.HomeListBean;
import yt.kpool.network.NetHelperNew;
import yt.kpool.util.Util;
import yt.kpool.util.imageUtil.GlideUtil;

/**
 * Created by wxy on 2017/4/27.
 */

public class SimpleAdapter extends BaseQuickAdapter<HomeListBean.DataBean.MinerModelListBean, BaseViewHolder> {
    private Context context;
    private Double Exchange;
    private int kind,type;
    private double flash_p=0.00;//电费
    public SimpleAdapter(Context context,@Nullable List<HomeListBean.DataBean.MinerModelListBean> data) {
        super(R.layout.item_layout, data);
        this.context=context;

    }

    @Override
    protected void convert(BaseViewHolder helper, final HomeListBean.DataBean.MinerModelListBean item) {
        TextView tv=helper.getView(R.id.tv_view);
       if(helper.getAdapterPosition()== getData().size())
        {
          tv.setVisibility(View.VISIBLE);
        }
      else
        {
          tv.setVisibility(View.GONE);
        }
        GlideUtil.loadImage(context, NetHelperNew.URL + item.getIcon(), (ImageView) helper.getView(R.id.iv_icon), R.mipmap.ic_launcher, R.mipmap.ic_launcher);
        helper.setText(R.id.tv_name, item.getName());
        helper.setText(R.id.tv_Currency, item.getCurrency()+"");


        BigDecimal c1 = new BigDecimal(item.getCurrencyPrice());
        BigDecimal c2 = new BigDecimal(Exchange);
        String cp1=c1.multiply(c2).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue()+"";



        BigDecimal b1 = new BigDecimal(item.getThroughput());
        BigDecimal b2 = new BigDecimal(cp1);
        String sp1=b1.multiply(b2).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue()+"";


        BigDecimal bb1 = new BigDecimal(item.getPower());
        BigDecimal bb2 = new BigDecimal(Util.NMultiplication(0.024+"",flash_p+""));
        String sp2=bb1.multiply(bb2).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue()+"";


        TextView textView=helper.getView(R.id.tv_price);
        BigDecimal bbb1 = new BigDecimal(sp1);
        BigDecimal bbb2 = new BigDecimal(sp2);
        String sp3=bbb1.subtract(bbb2).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue()+"";
        TextView tv_d1=helper.getView(R.id.tv_d1);
        TextView tv_d2=helper.getView(R.id.tv_d2);
        TextView tv_CurrencyPrice=helper.getView(R.id.tv_CurrencyPrice);
        TextView tv_CurrencyPrice2=helper.getView(R.id.tv_CurrencyPrice2);
        LinearLayout ly2=helper.getView(R.id.ly2);
        LinearLayout ly22=helper.getView(R.id.ly22);

        helper.setText(R.id.tv_datePay2, item.getHashrate()+"");
        helper.setText(R.id.tv_d11, item.getHashrateUnit());
        helper.setText(R.id.tv_flsahay2, "/"+item.getPower());
        helper.setText(R.id.tv_d22, "W");

        double b=(double) item.getPower()/(double) item.getHashrate();
        tv_CurrencyPrice2.setText(new BigDecimal(b+"").setScale( 0, BigDecimal.ROUND_UP )+"W/"+item.getHashrateUnit());//四舍五入 取整

        String sp33=Util.Subtraction(Util.Transformation(sp1),sp2);
        if(kind==1)//人民币
        {
            helper.setText(R.id.tv_CurrencyPrice, "¥"+cp1);
            helper.setText(R.id.tv_datePay, sp1+"/");
            helper.setText(R.id.tv_flsahay, sp2);
            textView.setText("¥"+sp3);
            tv_d1.setText("¥");
            tv_d2.setText("¥");

        }
        else if (kind==2)//美元
        {
            helper.setText(R.id.tv_CurrencyPrice, "$"+ Util.Transformation(cp1));
            helper.setText(R.id.tv_datePay, Util.Transformation(sp1)+"/");
            helper.setText(R.id.tv_flsahay, sp2);
            textView.setText("$"+sp33);
            tv_d1.setText("$");
            tv_d2.setText("$");
        }
        if(type==1)
        {   tv_CurrencyPrice.setVisibility(View.VISIBLE);
            ly2.setVisibility(View.VISIBLE);
            ly22.setVisibility(View.GONE);
            tv_CurrencyPrice2.setVisibility(View.GONE);
           if(kind==1)
           {
               textView.setText("¥"+sp3);
           }
           else
           {
               textView.setText("$"+Util.Subtraction(Util.Transformation(sp1),sp2));
           }

        }
        else if(type==2)
        {
            tv_CurrencyPrice.setVisibility(View.GONE);
            ly2.setVisibility(View.GONE);
            ly22.setVisibility(View.VISIBLE);
            tv_CurrencyPrice2.setVisibility(View.VISIBLE);
            if(kind==1)
            {


                textView.setText("¥"+ShutDownComputer(item));
            }
            else
            {

                textView.setText("$"+ShutDownComputer(item));
            }

        }
        if(kind==1)
        {
            if(Double.valueOf(sp3)>=10)
            {
                textView.setBackground(context.getResources().getDrawable(R.drawable.button_green_bg));
            }
            else if(Double.valueOf(sp3)<10&&Double.valueOf(sp3)>=5)
            {
                textView.setBackground(context.getResources().getDrawable(R.drawable.button_orange_bg));
            }
            else if(Double.valueOf(sp3)<5&&Double.valueOf(sp3)>=0)
            {
                textView.setBackground(context.getResources().getDrawable(R.drawable.button_oranges_bg));
            }
            else if(Double.valueOf(sp3)<0)
            {
                textView.setBackground(context.getResources().getDrawable(R.drawable.button_red_bg));
            }
        }
        else
        {
            if(Double.valueOf(sp33)>=1.42)
            {
                textView.setBackground(context.getResources().getDrawable(R.drawable.button_green_bg));
            }
            else if(Double.valueOf(sp33)<1.42&&Double.valueOf(sp33)>=0.71)
            {
                textView.setBackground(context.getResources().getDrawable(R.drawable.button_orange_bg));
            }
            else if(Double.valueOf(sp33)<0.71&&Double.valueOf(sp33)>=0)
            {
                textView.setBackground(context.getResources().getDrawable(R.drawable.button_oranges_bg));
            }
            else if(Double.valueOf(sp33)<0)
            {
                textView.setBackground(context.getResources().getDrawable(R.drawable.button_red_bg));
            }
        }


        helper.getConvertView().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
//                context.startActivity(new Intent(context, HomeSecondAc.class));
                HomeSecondAc.start(context,item.getID()+"",item.getName());
            }
        });
    }

    /**
     * 设置电费
     * @param Exchange
     */
    public void setExchange(Double Exchange)
    {
        this.Exchange=Exchange;
    }

    /**
     * 设置币种
     * @param kind
     */
    public void setKind(int kind,double flash_p)
    {
        this.kind=kind;
        this.flash_p=flash_p;

    }

    /**
     * 切换
     * @param type
     */
    public void setChange(int type)
    {

        this.type=type;

    }

    /**
     * 关机币价
     * @param item
     * @return
     */
    private String ShutDownComputer(HomeListBean.DataBean.MinerModelListBean item)
    {
        double m=(double) item.getPower()*0.024*flash_p/(double) item.getThroughput();
        return  Util.GetTNumber(m+"");
    }
}
